//===============================================================================
// Microsoft Architecture Strategy Team
// LitwareHR - SaaS Sample Application
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================


//===============================================================================
// Microsoft Architecture Strategy Team
// LitwareHR - S+S Sample Application
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

using System;
using System.Windows;
using Microsoft.Practices.CompositeUI;
using Microsoft.Practices.CompositeUI.SmartParts;
using Microsoft.Practices.CompositeUI.WPF;
using Microsoft.Practices.ObjectBuilder;
using LitwareHR.Infrastructure.Interface;

namespace LitwareHR.PublicApp.WinUX
{
    /// <summary>
    /// Interaction logic for LoginView.xaml
    /// </summary>
    public partial class LoginView : System.Windows.Controls.UserControl, ILoginView, IDisposable
    {
        /// <summary>
        /// Initialize a new instance of <see cref="LoginView"/>.
        /// <summary>
        public LoginView()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(OnLoad);
            this.btnSubmit.Click += new RoutedEventHandler(btnSubmit_Click);
            
        }
        /// <summary>
        /// This method reads Tenant Alias, User Name & Password from respective textboxes and Validates it.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void btnSubmit_Click(object sender, RoutedEventArgs e)
        {
            if (IsValid())
            {
                _presenter.ValidateUser(txtTenantTextBox.Text, txtUserTextBox.Text, txtPasswordTextBox.Password);
            }
        }

        private bool IsValid()
        {
            if (String.IsNullOrEmpty(txtTenantTextBox.Text))
            {
                ShowMessage(Properties.Resources.TenantAliasRequired);
                return false;
            }

            if (String.IsNullOrEmpty(txtUserTextBox.Text))
            {
                ShowMessage(Properties.Resources.UserNameRequired);
                return false;
            }

            if (String.IsNullOrEmpty(txtPasswordTextBox.Password))
            {
                ShowMessage(Properties.Resources.PasswordRequired);
                return false;
            }

            return true;
        }

        /// <summary>
        /// Called when WPF has loaded the control and is ready to display it.
        /// <summary>
        /// <param name="sender">Source of the event</param>
        /// <param name="e">Extra information about the event</param>
        void OnLoad(object sender, RoutedEventArgs e)
        {
            _presenter.OnViewReady();
        }

        #region ILoginView Members

        public void ShowMessage(string message)
        {
            txtMessageBox.Text = message;
        }

        public void LoadResources()
        {
            txtTenantText.Text = Properties.Resources.LoginView_TenantId;
            txtUserText.Text = Properties.Resources.LoginView_UserId;
            txtPasswordText.Text = Properties.Resources.LoginView_Password;

            // Merge resoure directory
            base.Resources.MergedDictionaries.Add((ResourceDictionary)Application.LoadComponent(new Uri(@"Litware.HR;;;component/Views/Skins/BaseSkin.xaml", UriKind.Relative)));
        }

        #endregion
    }
}

